app.controller("z-retroactiveTimes-c", [ '$scope','$rootScope','$z-http','$sce','z-retroactiveTimes-s','z-edit-menu-s','$filter',
		function($scope,$rootScope,$zhttp,$sce,Service,MenuService,$filter) {
		$scope.viewModel = {};
		$scope.editModel = {};
		$scope.editModel.title="";
		$scope.editModel.retroactiveTimes={};
		$scope.viewModel.yearData = [];
		var newYear = new Date().getFullYear();
		for(var year = newYear-10;year<=newYear+10;year++ ){
			$scope.viewModel.yearData.push(year);
		}
		//查询页面名称
		MenuService.getMenuByUrl({url:'retroactiveTimes'},
		function(data){	
			$scope.viewModel.menuList=data;
		},function(error) {});
		
		//加载新增页月份数据
		$scope.editModel.bindSelectData = function(){
			$scope.editModel.MonthData = $scope.editModel.canUseMonth;
		};
		
		//加载数据
		$scope.viewModel.bindData = function(){
			$scope.viewModel.isLoading=true;
			//获取基本信息
			Service.getRetroactiveTimesList({
			},
			function(data){
				$scope.viewModel.dataList = data;
				$scope.viewModel.isLoading=false;
			},function(error) {
				$scope.viewModel.isLoading=false;
			});
		};
		
		$scope.viewModel.bindData();
		
		//加载
		$scope.viewModel.load=function(){
			$scope.viewModel.show=true;
		};
		
		//关闭加载
		$scope.viewModel.unLoad=function(){
			$scope.viewModel.show=false;
		};
		
		//新增
		$scope.viewModel.add = function(){
			$scope.editModel.title="新增补签次数";
			$scope.editModel.type = true;
			$scope.editModel.retroactiveTimes={};
			$scope.editModel.retroactiveTimes.year = newYear+"";
			$scope.viewModel.load();
			//验证不能为空
			$scope.editForm.month.$dirty=false;
		};
		
		//修正
		$scope.viewModel.update=function(retroactiveTimes){
			$scope.editModel.title="修改补签次数";
			$scope.editModel.type = false;
			$scope.editModel.retroactiveTimes=angular.copy(retroactiveTimes);
			$scope.viewModel.load();
		};
		
		//验证
		$scope.viewModel.validForm= function() {
            if (!$scope.editForm.$valid) {
            	if(!$scope.editForm.month.$dirty){
            		$scope.editForm.month.$dirty=true;
            	}
                return true;
            }else{
            	return false;
            }
        };
		
		//保存
		$scope.viewModel.save = function(){
			if($scope.viewModel.validForm()){
				return;
			}
			$scope.viewModel.isLoading = true;
			if($scope.editModel.type){
				Service.saveRetroactiveTimes($scope.editModel.retroactiveTimes,
						function(data){
							$scope.viewModel.unLoad();
							$scope.viewModel.bindData();
							$rootScope.alertInfo("设置成功。");
							$scope.viewModel.isLoading = false;
						},function(error) {
							$rootScope.alertError(error.fault.message);
							$scope.viewModel.isLoading = false;
						});
			}else{
				Service.updateRetroactiveTimes($scope.editModel.retroactiveTimes,
						function(data){
							$scope.viewModel.unLoad();
							$scope.viewModel.bindData();
							$rootScope.alertInfo("修改成功。");
							$scope.viewModel.isLoading = false;
						},function(error) {
							$rootScope.alertError(error.fault.message);
							$scope.viewModel.isLoading = false;
						});
			}
		};
		
		//删除
		$scope.viewModel.delele=function(retroactiveTimes){
			$rootScope.alertConfirm("确定删除吗？",function() {
				$scope.viewModel.isLoading=true;
				Service.deleteRetroactiveTimes({
					retroactiveTimesId:retroactiveTimes.retroactiveTimesId
				},function(){
					$scope.viewModel.bindData();
					$scope.viewModel.isLoading=false;
					$rootScope.alertInfo("删除成功。");
				},function(error){
					$scope.viewModel.isLoading=false;
					$rootScope.alertError(error.fault.message);
				});
			});
		};
} ]);